<?php

use App\Models\System\Menus;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use App\Models\System\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UserSeeder extends Seeder
{
    private $role;

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->truncateTable();
        $username = 'root';$password = 'root123123';
        $user = User::query()->create([
            'avatar' => 'static/admin/img/default_headimg.gif',
            'username' => $username,
            'display_name' => '超级管理员',
            'password' => bcrypt($password),
            'status' => 1,
            'uuid' => \Faker\Provider\Uuid::uuid()
        ]);
        $this->role = Role::query()->create([
            'name' => 'root',
            'guard_name' => 'admin',
            'display_name' => '超级管理组'
        ]);
        $user->assignRole($this->role);
        $this->dealWitchMenus($this->menusData);
        echo '----------------------------------'."\n";
        echo "url: ".config('app.url')." \n";
        echo 'username: '.$username."\n";
        echo 'password: '.$password."\n";
        echo '----------------------------------'."\n";
    }

    private function truncateTable()
    {
        $tableNames = config('permission.table_names');
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        DB::table($tableNames['model_has_permissions'])->truncate();
        DB::table($tableNames['model_has_roles'])->truncate();
        DB::table($tableNames['role_has_permissions'])->truncate();
        DB::table($tableNames['roles'])->truncate();
        DB::table($tableNames['permissions'])->truncate();
        DB::table('users')->truncate();
        DB::table('system_menus')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }

    private function dealWitchMenus(array $permissions, int $parentId = 0)
    {
        if (!empty($permissions)) {
            foreach ($permissions as $pem) {
                $p = $this->createMenus($pem, $parentId);
                if (!empty($pem['child'])) {
                    $this->dealWitchMenus($pem['child'], $p['id']);
                }
            }
        }
    }

    private function createMenus(array $data, int $parentId)
    {
        $this->createPermission($data);
        return Menus::query()->create([
            'name'          => trim($data['name']),
            'display_name'  => trim($data['display_name']),
            'url'           => $data['route'] ?? '',
            'icon'          => $data['icon'] ?? '',
            'genre'         => (int)$data['genre'],
            'parent_id'     => $parentId,
            'status'        => 1,
        ]);
    }

    private function createPermission(array $data)
    {
        $item = Permission::query()->create([ "name" => trim($data['name']) ]);
        //为角色添加权限
        $this->role->givePermissionTo($item);
    }

    private $menusData = [
        [
            'name' => 'content.manage',
            'display_name' => '内容管理',
            'route' => '',
            'icon' => 'layui-icon layui-icon-read',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'content.kv',
                    'display_name' => '广告管理',
                    'route' => 'content/kv/index',
                    'icon' => 'layui-icon layui-icon-util',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.kv.create', 'display_name' => '添加广告', 'genre' => 2],
                        ['name' => 'content.kv.edit', 'display_name' => '编辑广告', 'genre' => 2],
                        ['name' => 'content.kv.destroy', 'display_name' => '删除广告', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.articles',
                    'display_name' => '文章管理',
                    'route' => 'content/articles/index',
                    'icon' => 'layui-icon layui-icon-form',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.articles.create', 'display_name' => '添加文章', 'genre' => 2],
                        ['name' => 'content.articles.edit', 'display_name' => '编辑文章', 'genre' => 2],
                        ['name' => 'content.articles.destroy', 'display_name' => '删除文章', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.column',
                    'display_name' => '栏目管理',
                    'route' => 'content/column/index',
                    'icon' => 'layui-icon layui-icon-util',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.column.create', 'display_name' => '添加栏目', 'genre' => 2],
                        ['name' => 'content.column.edit', 'display_name' => '编辑栏目', 'genre' => 2],
                        ['name' => 'content.column.destroy', 'display_name' => '删除栏目', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.attachment',
                    'display_name' => '附件管理',
                    'route' => 'content/attachment/index',
                    'icon' => 'layui-icon layui-icon-file-b',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.attachment.upload', 'display_name' => '上传附件', 'genre' => 2],
                        ['name' => 'content.attachment.destroy', 'display_name' => '删除附件', 'genre' => 2],
                        ['name' => 'content.attachment.recycle', 'display_name' => '回收站', 'genre' => 2],
                        ['name' => 'content.attachment.recover', 'display_name' => '回收站恢复', 'genre' => 2],
                        ['name' => 'content.attachment.expurgate', 'display_name' => '回收站删除', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'content.attachmentGroup',
                    'display_name' => '附件分组',
                    'route' => 'content/attachmentGroup/index',
                    'icon' => 'layui-icon layui-icon-file',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'content.attachmentGroup.create', 'display_name' => '添加分组', 'genre' => 2],
                        ['name' => 'content.attachmentGroup.edit', 'display_name' => '编辑分组', 'genre' => 2],
                        ['name' => 'content.attachmentGroup.destroy', 'display_name' => '删除分组', 'genre' => 2],
                    ]
                ],
            ]
        ],
        [
            'name' => 'system.manage',
            'display_name' => '系统管理',
            'route' => '',
            'icon' => 'layui-icon layui-icon-util',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'system.user',
                    'display_name' => '账号管理',
                    'route' => 'system/user/index',
                    'icon' => 'layui-icon layui-icon-friends',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.user.create', 'display_name' => '添加账号', 'genre' => 2],
                        ['name' => 'system.user.edit', 'display_name' => '编辑账号', 'genre' => 2],
                        ['name' => 'system.user.destroy', 'display_name' => '删除账号', 'genre' => 2],
                        ['name' => 'system.user.resetPassword', 'display_name' => '重置密码', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.role',
                    'display_name' => '角色管理',
                    'route' => 'system/role/index',
                    'icon' => 'layui-icon layui-icon-set',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.role.create', 'display_name' => '添加角色', 'genre' => 2],
                        ['name' => 'system.role.edit', 'display_name' => '编辑角色', 'genre' => 2],
                        ['name' => 'system.role.destroy', 'display_name' => '删除角色', 'genre' => 2],
                        ['name' => 'system.role.permission', 'display_name' => '分配权限', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.menus',
                    'display_name' => '菜单管理',
                    'route' => 'system/menus/index',
                    'icon' => 'layui-icon layui-icon-auz',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.menus.create', 'display_name' => '添加菜单', 'genre' => 2],
                        ['name' => 'system.menus.edit', 'display_name' => '编辑菜单', 'genre' => 2],
                        ['name' => 'system.menus.destroy', 'display_name' => '删除菜单', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'system.config',
                    'display_name' => '系统设置',
                    'route' => 'system/config/index',
                    'icon' => 'layui-icon layui-icon-website',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'system.config.update', 'display_name' => '更新设置', 'genre' => 2],
                    ]
                ],
            ]
        ],
        [
            'name' => 'maintain.manage',
            'display_name' => '系统监控',
            'route' => '',
            'icon' => 'layui-icon layui-icon-app',
            'genre' => 1,
            'child' => [
                [
                    'name' => 'maintain.database',
                    'display_name' => '数据表维护',
                    'route' => 'maintain/database/index',
                    'icon' => 'layui-icon layui-icon-set',
                    'genre' => 1,
                    'child' => [
                        ['name' => 'maintain.database.optimize', 'display_name' => '优化表', 'genre' => 2],
                        ['name' => 'maintain.database.repair', 'display_name' => '修复表', 'genre' => 2],
                        ['name' => 'maintain.database.clear', 'display_name' => '清空表', 'genre' => 2],
                    ]
                ],
                [
                    'name' => 'maintain.logs',
                    'display_name' => '日志监控',
                    'route' => '',
                    'icon' => 'layui-icon layui-icon-log',
                    'genre' => 1,
                    'child' => [
                        [
                            'name' => 'maintain.logs.login',
                            'display_name' => '登陆日志',
                            'route' => 'maintain/logs/login/index',
                            'icon' => 'layui-icon layui-icon-log',
                            'genre' => 1,
                            'child' => []
                        ],
                        [
                            'name' => 'maintain.logs.access',
                            'display_name' => '访问日志',
                            'route' => 'maintain/logs/access/index',
                            'icon' => 'layui-icon layui-icon-log',
                            'genre' => 1,
                            'child' => []
                        ],
                        [
                            'name' => 'maintain.logs.activity',
                            'display_name' => '操作日志',
                            'route' => 'maintain/logs/activity/index',
                            'icon' => 'layui-icon layui-icon-log',
                            'genre' => 1,
                            'child' => []
                        ],
                    ]
                ],
            ]
        ],
    ];

}
